Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: introduce base HighlighterTestCase, dedicated tests for the getCodeSnippet() + two bug fixes #35

Merged
merged 4 commits into from
Feb 4, 2022

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Jan 28, 2022

Tests: introduce base HighlighterTestCase

... from which all test classes should extend and which contains the getConsoleColorMock() method to prevent having to duplicate it in multiple places.

The method has been adjusted to be slightly more flexible for re-use, in that the $withTheme parameter can be used to determine whether the ConsoleColor class should behave as if themes have been registered or not.

Tests: add dedicated tests for the getCodeSnippet() method

These tests also test the private getHighlightedLines(), splitToLines() and lineNumbers() methods and the data sets in the data provider have been set up to ensure those methods are tested thoroughly.

Highlighter::getCodeSnippet(): bug fix - too many lines retrieved at start of file

When a code snippet in the middle of code is retrieved, the target snippet length calculation $length = $linesAfter + $linesBefore + 1; functions correctly, displaying the target line in the middle, padded by x number of lines before and after.
Given $linesBefore and $linesAfter both being set to two, this would display as:

before
before
target
after
after

Similarly, when a code snippet at the end of the code is retrieved, it will work correctly as well, the target line is displayed with x number of lines before. The number of lines after is limited automatically by the length of the $tokenLines array.
Given $linesBefore and $linesAfter both being set to two, this would display as:

before
before
target

However, when a code snippet at the start of the code is retrieved, the number of lines retrieved was incorrect. The target line would display with ($linesBefore + $linesAfter) lines after the target line.

Given $linesBefore and $linesAfter both being set to two, this currently displays as:

target
after
after
after
after

... while it should be:

target
after
after

The change in this commit fixes this bug.

Highlighter::lineNumbers(): bug fix - remove stray blank line at end of output

The lineNumbers() functionality adds a new line marker at the end of each line, including the last line, meaning that any code snippets being passed through this function will have a stray blank line at the end of the output.

Fixed now.

jrfnl added 4 commits January 28, 2022 09:31
... from which all test classes should extend and which contains the `getConsoleColorMock()` method to prevent having to duplicate it in multiple places.

The method has been adjusted to be slightly more flexible for re-use, in that the `$withTheme` parameter can be used to determine whether the ConsoleColor class should behave as if themes have been registered or not.
These tests also test the `private` `getHighlightedLines()`,  `splitToLines()` and `lineNumbers()` methods and the data sets in the data provider have been set up to ensure those methods are tested thoroughly.
…start of file

When a code snippet in the middle of code is retrieved, the target snippet length calculation `$length = $linesAfter + $linesBefore + 1;` functions correctly, displaying the target line in the middle, padded by x number of lines before and after.
Given `$linesBefore` and `$linesAfter` both being set to two, this would display as:
```
before
before
target
after
after
```

Similarly, when a code snippet at the end of the code is retrieved, it will work correctly as well, the target line is displayed with x number of lines before. The number of lines _after_ is limited automatically by the length of the `$tokenLines` array.
Given `$linesBefore` and `$linesAfter` both being set to two, this would display as:
```
before
before
target
```

However, when a code snippet at the _start_ of the code is retrieved, the number of lines retrieved was incorrect. The target line would display with `($linesBefore + $linesAfter)` lines _after_ the target line.

Given `$linesBefore` and `$linesAfter` both being set to two, this currently displays as:
```
target
after
after
after
after
```

... while it should be:
```
target
after
after
```

The change in this commit fixes this bug.
…of output

The `lineNumbers()` functionality adds a new line marker at the end of each line, including the last line, meaning that any code snippets being passed through this function will have a stray blank line at the end of the output.

Fixed now.
@jrfnl jrfnl added this to the 1.0.0 milestone Jan 28, 2022
@jrfnl jrfnl requested a review from grogy January 28, 2022 08:42
@grogy grogy merged commit 6e51cb1 into master Feb 4, 2022
@grogy
Copy link
Member

grogy commented Feb 4, 2022

Merged, thank you :-)

@jrfnl jrfnl deleted the feature/tests-add-tests-for-getcodesnippet branch February 4, 2022 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants